<template>
  <Layout>
    <Form ref="form1" :model="dataToQuery" :label-width="120">
      <Row class="query-form">
        <Col span="8">
          <FormItem label="二级集团：">
            <two-level-org org-code class="query-form-el" v-model="dataToQuery.twoLevelOrgCode"/>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label="单位名称：" prop="order">
            <!--                        <cascader-dom class="query-form-el" ref='queryOrgCascader' @setOrgInfo='setQueryCompanyName' :orgTree="true"-->
            <!--                                      :setCurrentName="{label:dataToQuery.companyName,value:dataToQuery.companyId}"></cascader-dom>-->
            <Input class="query-form-el" type="text" v-model="dataToQuery.companyName"></Input>
          </FormItem>
        </Col>
        <Col span="8" v-if="!switchFlag">
          <Button @click="switchQuery(true,88)" class="ivu-btn-switch" type="text">展开∨</Button>
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
        <Col span="8" v-else>
          <FormItem label="当事人：" prop="driver">
            <Input class="query-form-el" type="text" v-model="dataToQuery.driver"></Input>
          </FormItem>
        </Col>
      </Row>
      <Row v-show="switchFlag" class="query-form">
        <Col span="8">
          <FormItem label="号牌号码：" prop="plateNumber">
            <Input class="query-form-el" type="text" v-model="dataToQuery.plateNumber"></Input>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label='违法时间：'>
            <DatePicker class="query-form-el" v-model="dataToQuery.violationDateRange" format="yyyy-MM-dd"
                        type="daterange" placement="bottom-start" placeholder="请选择检查时间段"
                        @on-change="(date) => onSelectFieldQueryDateRange(date, 'violationDate')"></DatePicker>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label="驾驶证号：" prop="driverNumber">
            <Input class="query-form-el" type="text" v-model="dataToQuery.driverNumber"></Input>
          </FormItem>
        </Col>
      </Row>
      <Row v-show="switchFlag" class="query-form">
        <Col span="8" offset="16">
          <Button @click="switchQuery(false,88)" class="ivu-btn-switch" type="text">收起∧</Button>
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
      <Row class="query-form">
        <Col span="24">
          <Button size="large" class="ivu-btn-add" icon="md-add" @click="addInfo">新增</Button>
          <download-button :api="exportUrl" :query-form="dataToQuery"/>
          <!--<Button type="primary" class="ivu-btn-add" icon="md-cloud-download" @click="exportFormSubmit">导出</Button> -->
        </Col>
      </Row>
    </Form>
    <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据!"
           @on-sort-change="sortChange" @on-selection-change='getSelectRows' :loading='pageStatus.loading' border
           :columns="columns1" :data="dataTable">
    </Table>
    <Row style="margin-right:10px;">
      <Col span="24">
        <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange"
              @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer
              show-elevator/>
      </Col>
    </Row>
    <Modal :title="this.getModalTitle('私家车醉驾台账')" :transfer=false :mask-closable="false" v-model="dialogShow"
           v-if="dataToView && dialogShow" :width=800 class-name='limit-height-dialog'>
      <Row>
        <Col span="24">
          <Form ref="dialogForm" :class="mode === 'view'?'modal-view-detail':''" :show-message="false"
                :model="dataToView" :rules="mode === 'view'?{}:ruleCustom" :label-width="mode === 'view'?110:null"
                :label-position="mode === 'view'?'right':'top'">
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="11">
                <FormItem label='单位名称：' prop='companyId'>
                  <div v-if="mode === 'view'">{{ dataToView.companyName }}</div>
                  <div v-else>
                    <cascader-dom v-if="show" class='query-form-el' ref='orgCascader' @setOrgInfo='setCompanyId'
                                  @setOrgname="setCompanyName"
                                  @setOrgCode='setCompanyCode' :orgTree="true"
                                  :currentOrgId="dataToView.companyId" :rootOrgId="dataToView.companyId"
                                  :setCurrentName="{label:dataToView.companyName,value:dataToView.companyId}"></cascader-dom>
                  </div>
                </FormItem>
              </Col>
              <Col span="11">
                <FormItem label='所属二级集团：'>
                  <div v-if="mode === 'view'">{{ dataToView.belongOrgName }}</div>
                  <Select v-else class='query-form-el' placeholder="请填写所属二级集团"
                          v-model='dataToView["belongOrgId"]' :key="selectkey">
                    <Option v-for="item in twoLevel" :value="item.orgId" :key="item.orgId">{{
                        item.orgName
                      }}
                    </Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="11">
                <FormItem label='当事人：' prop='driver'>
                  <div v-if="mode === 'view'">{{ dataToView.driver }}</div>
                  <Input v-else class='query-form-el' placeholder="请填写当事人" type='text'
                         v-model='dataToView.driver'></Input>
                </FormItem>
              </Col>
              <Col span="11">
                <FormItem label='驾驶证号：' prop='driverNumber'>
                  <div v-if="mode === 'view'">{{ dataToView.driverNumber }}</div>
                  <Input v-else class='query-form-el' placeholder="请填写驾驶证号" type='text'
                         v-model='dataToView.driverNumber'></Input>
                </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="11">
                <FormItem label='号牌号码：' prop='plateNumber'>
                  <div v-if="mode === 'view'">{{ dataToView.plateNumber }}</div>
                  <Input v-else class='query-form-el' placeholder="请填写号牌号码" type='text'
                         v-model='dataToView.plateNumber'></Input>
                </FormItem>
              </Col>
              <Col span="11">
                <FormItem label='号牌种类：' prop='carName'>
                  <div v-if="mode === 'view'">{{ dataToView.carName }}</div>
                  <Input v-else class='query-form-el' placeholder="请填写号牌种类" type='text'
                         v-model='dataToView.carName'></Input>
                </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="11">
                <FormItem label='违法行为：' prop='violationItem'>
                  <div v-if="mode === 'view'">{{ dataToView.violationItem }}</div>
                  <Input v-else class='query-form-el' placeholder="请填写违法行为" type='text'
                         v-model='dataToView.violationItem'></Input>
                </FormItem>
              </Col>
              <Col span="11">
                <FormItem label='违法时间：' prop='violationDate'>
                  <div v-if="mode === 'view'">{{ renderViewDateTime(dataToView.violationDate) }}</div>
                  <DatePicker v-else type="datetime" class="query-form-el" placeholder="请选择违法时间"
                              v-model='dataToView.violationDate'></DatePicker>
                </FormItem>
              </Col>
            </Row>

            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="23">
                <FormItem v-if="mode === 'view'" label="附件：">
                  <div v-show="dataToView.idFiles.length>0" v-for="(item,index) in dataToView.idFiles" :key="item.id">
                    <a @click="downloadFiles(item,index)"><span style="color: #3399ff;margin-left: 5px">{{
                        item.fileName
                      }}</span></a>
                    <Progress v-show="uploadListVisible[item.id]" :percent="uploadListPercent[index]"></Progress>
                  </div>
                </FormItem>
                <FormItem v-else label="附件：" prop="idFiles">
                  <div>
                    <Upload ref="upload"
                            :before-upload="(file) => { beforeUpload1(file, 'id', 'carDrunkDriving') }"
                            :show-upload-list="true"
                            :max-size="10240"
                            multiple
                            type="drag"
                            :action="fileUrl">
                      <div style="padding: 10px">
                        <Icon type="ios-cloud-upload" size="45" style="color: #3399ff"></Icon>
                        <p>点击将文件拖拽到这里上传</p>
                        <!--<span style="font-size:8px;color: #797a64">支持扩展名：.rar .zip .doc .docx .pdf...</span>-->
                      </div>
                    </Upload>
                  </div>
                  <div v-show="dataToView.idFiles.length>0" v-for="(item,index) in dataToView.idFiles" :key='item'>
                    <a @click="downloadFiles(item,index)">{{ item.fileName }}</a>
                    <Button type="text" icon="md-trash" @click="removeFile1(item.id, 'id', 'carDrunkDriving')"
                            style="margin-left: 5px;margin-bottom: 3px"/>
                    <Progress v-show="uploadListVisible[item.id]" :percent="uploadListPercent[index]"></Progress>
                    <!--<Progress  v-show="filePercentVisible[index]" :percent="filePercent[index]"></Progress>-->
                  </div>
                </FormItem>
              </Col>
            </Row>


            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="11">
                <FormItem label='填报人：' prop='createUserName'>
                  <div v-if="mode === 'view'">{{ dataToView.createUserName }}</div>
                  <Input v-else class='query-form-el' placeholder="请填写填报人" type='text' readonly
                         v-model='dataToView.createUserName'></Input>
                </FormItem>
              </Col>
              <Col span="11">
                <FormItem label='填报时间：' prop='createDate'>
                  <div v-if="mode === 'view'">{{ renderViewDate(dataToView.createDate) }}</div>
                  <DatePicker v-else class="query-form-el" placeholder="请选择填报时间" readonly
                              v-model='dataToView.createDate'></DatePicker>
                </FormItem>
              </Col>
            </Row>
          </Form>
        </Col>
      </Row>
      <div slot="footer">
        <Button v-if="mode != 'view'" type="primary" class='ivu-btn-save' @click="submit('dialogForm')">确定
        </Button>
        <Button v-if="mode != 'view'" type="primary" class='ivu-btn-cancel' @click="handleCancel">取消</Button>
        <Button v-if="mode === 'view'" class='ivu-btn-save' @click="dialogShow=false">关闭</Button>
      </div>
    </Modal>

  </Layout>
</template>

<script>
import {Api} from '@/api/index.js'
import pageCommonFunction from '@/components/pageCommonFunction.js'
import {sessionStorage} from '@/common/cacheUtils.js';

var cascaderDom = () => import('@/components/cascader-dom.vue');

export default {
  components: {cascaderDom},
  data() {
    return {
      ...this.createCommonDataField(),
      selectkey: 1,
      columns1: [
        {title: '序号', width: 70, fixed: 'left', align: 'center', render: this.indexColumnsRender},
        {key: 'companyName', title: '单位名称', align: 'center', minWidth: 180,},
        {key: 'driver', title: '当事人', align: 'center', minWidth: 120,},
        {key: 'driverNumber', title: '驾驶证号', align: 'center', minWidth: 120,},
        {key: 'plateNumber', title: '号牌号码', align: 'center', minWidth: 120,},
        {key: 'carName', title: '号牌种类', align: 'center', minWidth: 120,},
        {key: 'violationItem', title: '违法行为', align: 'center', minWidth: 120,},
        {
          key: 'violationDate',
          title: '违法时间',
          sortable: "custom",
          align: 'center',
          minWidth: 120,
          render: this.renderCellDateTime
        },
        {
          key: 'createDate',
          title: '填报时间',
          sortable: "custom",
          align: 'center',
          minWidth: 120,
          render: this.renderCellDate
        },
        {key: 'belongOrgName', title: '所属二级集团', align: 'center', minWidth: 180,},
        this.getActionColumns(),
      ],
      show: true,
      ruleCustom: {
        belongOrgId: [{required: true, message: '请填写所属二级集团名称', trigger: 'change',}],
        companyId: [{required: true, message: '请填写单位名称', trigger: 'change',}],
        driver: [{required: true, message: '请填写当事人', trigger: 'change',}],
        driverNumber: [{required: true, message: '请填写驾驶证号', trigger: 'change',}],
        plateNumber: [{required: true, message: '请填写号牌号码', trigger: 'change',}],
        carName: [{required: true, message: '请填写号牌种类', trigger: 'change',}],
        violationItem: [{required: true, message: '请填写违法行为', trigger: 'change',}],
        violationDate: [{required: true, message: '请填写违法时间', trigger: 'change', type: 'date'}],
        idFiles: [{
          validator: (rule, value, callback) => {
            if (this.dataToView.idFiles.length < 1) {
              callback('附件必须上传')
            } else {
              callback()
            }
          },
          required: true,
          trigger: 'change'
        }]
      },
      tableHeight: 10,
      // api
      queryUrl: Api.queryCarDrunkDriving,
      exportUrl: Api.exportCarDrunkDriving,
      deleteUrl: Api.deleteCarDrunkDriving,
      getUrl: Api.getCarDrunkDriving,
      updateUrl: Api.updateCarDrunkDriving,
      saveUrl: Api.saveCarDrunkDriving,
      currentUser: JSON.parse(sessionStorage('userId').get('userId')),
      twoLevel: [],
      options: {
        disabledDate(date) {
          return date && date.valueOf() >= new Date();
        }
      },
    }
  },
  mounted() {
    this.setTableHeight();
    this.query();
    this.selectSecGroupNames()
  },
  methods: {
    ...pageCommonFunction,
    setCompanyCode(orgCode) {
      this.selectkey++;
      this.twoLevel.forEach(item => {
        if (orgCode.indexOf(item.orgCode) != -1) {
          this.$set(this.dataToView, 'belongOrgId', item.orgId);
          this.$set(this.dataToView, 'belongOrgName', item.orgName);
          return;
        }
      })
    },
    selectSecGroupNames() {
      this.$http.get(Api.quqeryTwoOrgInfo).then(res => {
        this.twoLevel = res.data.data
      }).catch(e => {
      })
    },
    deleteData(params) {
      this.deleteDateAjax(params);
    },
    query() {
      this.queryAjax();
    },
    submit(name) {
      this.submitAjax(name);
    },
    createEmptyDataToAdd() {
      return {
        createDate: new Date(),
        createUserId: this.currentUser.userId,
        createUserName: this.currentUser.userName,
        id: this.get_uuid(),
        idFiles: [],
      }
    },
    async afterDataToViewInit() {
      await this.getFiles1('id', 'carDrunkDriving');
    },
  },
}
</script>

<style lang='css' scoped>
.ivu-form-item {
  padding-right: 20px;
}

.query-form-el, .ivu-date-picker, .ivu-input-wrapper {
  width: 100%;
}

</style>
